﻿Public Class frmCestas
    Dim objcontrole As New ClsControle
    Dim objCesta As New CLSCestas
    Dim objcestaitens As New CLSCestaProdutos
    Dim objProduto As New CLSProdutos
    Dim novo As Boolean
    Dim estoque As Integer
    Dim total, geral As Single

 
    Private Sub btnSalvar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSalvar.Click
        If Not objcontrole.Testar_vazio(Me) Then
            objCesta.Nome = txtNome.Text
            objCesta.valorTotalCesta = TxtGer.Text
            novo = True
            objCesta.Gravar(novo)
            txtCod.Text = objCesta.ID

            For linha = 0 To dgvItensCesta.RowCount - 1
                objcestaitens.idCesta = txtCod.Text
                objcestaitens.idProduto = dgvItensCesta.Item(0, linha).Value
                objcestaitens.qtdCestas = dgvItensCesta.Item(2, linha).Value
                objcestaitens.Gravar()
                MessageBox.Show("Dados da CESTA salvo com Sucesso!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Next
        End If


    End Sub

    Private Sub btnItens_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItens.Click
        dgvItensCesta.Rows.Add(cboNomeProd.SelectedValue, cboNomeProd.Text, txtQtdProduto.Text, cboValor.Text, total)
        geral = geral + total
        TxtGer.Text = geral
        txtQtdProduto.Clear()
        txtQtdProduto.Focus()

    End Sub

    Private Sub frmCestas_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        objcontrole.Habilitar_tela(Me, False)
        objcontrole.Habilitar_botoes(Me, False)
        btnNovo.Enabled = True
        btnAddItens.Enabled = True
        btnSair.Enabled = True
        btnCancelar.Enabled = True


        cboNomeProd.DisplayMember = "Pro_Nom"
        cboNomeProd.ValueMember = "Pro_ID"
        cboNomeProd.DataSource = objProduto.Localizarnome("")

        cboValor.DisplayMember = "Pro_VU"
        cboValor.ValueMember = "Pro_qtd"
        cboValor.DataSource = objProduto.LocalizarPorID(cboNomeProd.SelectedValue)
        estoque = cboValor.SelectedValue
    End Sub

    Private Sub cboNomeProd_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboNomeProd.SelectedIndexChanged
        cboValor.DisplayMember = "Pro_VU"
        cboValor.ValueMember = "Pro_qtd"
        cboValor.DataSource = objProduto.LocalizarPorID(cboNomeProd.SelectedValue)
        estoque = cboValor.SelectedValue
    End Sub

    Private Sub txtQtdProduto_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtQtdProduto.TextChanged

        If txtQtdProduto.Text <> "" Then
            If txtQtdProduto.Text > estoque Then
                MessageBox.Show("Quantidade indiponível , temos apenas " & estoque & " itens ")
                txtQtdProduto.Clear()
                txtQtdProduto.Focus()
            Else
                total = txtQtdProduto.Text * cboValor.Text
            End If
        End If
    End Sub

    Private Sub btnNovo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNovo.Click
        objcontrole.Limpar_tela(Me)
        objcontrole.Habilitar_tela(Me, True)
        'objcontrole.Habilitar_botoes(Me, False)
        btnSalvar.Enabled = True
        btnCancelar.Enabled = True
        txtNome.Focus()
        novo = True
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        frmCestas_Load(Nothing, Nothing)
        objcontrole.Limpar_tela(Me)
    End Sub

    Private Sub btnExcluir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExcluir.Click
        If objCesta.excluir(txtCod.Text) Then
            objcontrole.Limpar_tela(Me)
        End If
    End Sub

    Private Sub btnAlterar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAlterar.Click
        objcontrole.Habilitar_tela(Me, True)
        objcontrole.Habilitar_botoes(Me, False)
        txtCod.Enabled = False
        txtNome.Focus()
        novo = False
    End Sub

    Private Sub btnSair_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSair.Click
        Me.Close()
    End Sub

    Private Sub BTNPRI_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNPRI.Click
        objCesta.Ir_para_primeiro()
        'Call most()
    End Sub

    Private Sub BTNANT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNANT.Click

    End Sub
    Private Sub BTNULT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNULT.Click

    End Sub


End Class